﻿
namespace Huirui.Cavan.Presentation.Controller.AttachmentModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Huirui.Cavan.Presentation.Seedwork;
    using Huirui.Cavan.Domain.MainBoundedContext.AttachmentModule;
    using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
    using Huirui.Cavan.Application.MainBoundedContext.AttachmentModule;
    using System.Web;
    using System.Drawing;
    using System.IO;
    using Huirui.Cavan.Domain.Seedwork;
    using Huirui.Cavan.Presentation.ViewModel.AttachmentModule;
    using Huirui.Cavan.Application.MainBoundedContext.Setting;

    public class AttachmentController : EntityController<Attachment, AttachmentSearch, IAttachmentViewModel, IAttachmentAppService>, IAttachmentController
    {
        private IFileUploadAppService FileUploadAppService { get; set; }

        public AttachmentController()
        {
            this.Model = ModelFactory.CreateModel<IAttachmentViewModel>();
        }

        public override string ControllerName
        {
            get { return ControllerConst.ATTACHMENT; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        [ActionName(ActionConst.UPLOAD)]
        public JsonResult FileUpload(int id)
        {
            HttpPostedFileBase file = Request.Files[0];
            Image image = Image.FromStream(file.InputStream);
            string directory = FileUploadAppService.GetDirectory(id);
            string fileName = FileUploadAppService.GetNewFileName(image.Width, image.Height, FileUploadAppService.GetSaveFileSuffix(id));
            FileUploadRequest fuRequest = GetFileUploadRequest(id, directory, fileName, file.InputStream);
            FileUploadAppService.UploadFile(fuRequest);
            BoolResult result = new BoolResult();
            result.Messages.Add(string.Format(HuiruiProject.Settings.TempImageServer, 1) + directory.Replace("\\", "/") + fileName);

            return Json(result, "text/html; charset=utf-8");
        }

        [ActionName(ActionConst.UPLOADFROMXH)]
        public JsonResult FileUploadFromXH(int id)
        {
            Stream Stream;
            string disposition = HttpContext.Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];
            byte[] file;                     // 统一转换为byte数组处理
            if (disposition != null)
            {
                // HTML5上传
                file = HttpContext.Request.BinaryRead(HttpContext.Request.TotalBytes);
                Stream = new MemoryStream(file);
            }
            else
            {
                Stream = HttpContext.Request.Files[0].InputStream;
            }
            Image image = Image.FromStream(Stream);
            string directory = FileUploadAppService.GetDirectory(id);
            string fileName = FileUploadAppService.GetNewFileName(image.Width, image.Height, FileUploadAppService.GetSaveFileSuffix(id));
            FileUploadRequest fuRequest = GetFileUploadRequest(id, directory, fileName, Stream);
            FileUploadAppService.UploadFile(fuRequest);
            FileUploadAppService.CopyFile(directory.Replace("\\", "/"), fileName, string.Empty);
            ImageJsonViewModel result = new ImageJsonViewModel();
            result.msg = string.Format(HuiruiProject.Settings.ImageServer, 1) + directory.Replace("\\", "/") + fileName + "_360@360.jpg";

            return Json(result, "text/html; charset=utf-8");
        }

        private FileUploadRequest GetFileUploadRequest(int attachmentGroupID, string directory, string fileName, Stream stream)
        {
            stream.Position = 0;
            FileUploadRequest fuRequest = new FileUploadRequest();
            fuRequest.AttachmentGroupID = attachmentGroupID;
            fuRequest.FileDirectory = directory;
            fuRequest.FileName = fileName;
            fuRequest.FileData = stream;
            return fuRequest;
        }
    }
}
